/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.transas.zornav.file;

import com.transas.navmath.Pos3D;
import com.transas.zornav.data.DataSource;
import java.io.BufferedReader;
import java.io.Reader;
import java.util.Locale;
import java.util.Scanner;
import java.util.logging.Logger;

/**
 *
 * @author amalikov
 */
public class MissionPlannerDataSourceReader implements DataSourceReader {
	private final static Logger LOG = Logger.getLogger(MissionPlannerDataSourceReader.class.getName());

	@Override
	public void process(Reader file, DataSource ds) {
		Scanner s = new Scanner(new BufferedReader(file));
		s.useLocale(Locale.US);
		s.nextLine();	// skip header
		s.nextLine();	// skip home point
		
		int i = ds.getSize();
		Pos3D wpt;
		while ((wpt = getNextWayPoint(s)) != null) {
			ds.addRow();
			ds.setValue(ds.getHandle("N"), i, i);
			ds.setValue(ds.getHandle("Latitude"), i, wpt.lat);
			ds.setValue(ds.getHandle("Longitude"), i, wpt.lon);
			ds.setValue(ds.getHandle("Altitude"), i, wpt.alt);
			++i;
		}
	}
	
	private Pos3D getNextWayPoint(Scanner s) {
		double lat = 0;
		double lon = 0;
		double alt = 0;
	
		int i = 0;
		while (s.hasNext()) {
			double t = s.nextDouble();
			
			switch (i) {
				case 8:
					lat = t;
					break;
				case 9:
					lon = t;
					break;
				case 10:
					alt = t;
					break;
				case 11:
					return new Pos3D(lat, lon, alt);
			}
			
			++i;
		}
		
		return null;
	}
}
